package com.docmanage.am.service.impl;

import com.docmanage.am.domain.AmCabinet;
import com.docmanage.am.domain.CabinetTreeSelect;
import com.docmanage.am.mapper.AmCabinetMapper;
import com.docmanage.common.utils.DateUtils;
import com.docmanage.common.utils.SecurityUtils;
import com.docmanage.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 档案柜Service业务层处理
 * 
 * @author pointLion
 * @date 2023-06-18
 */
@Service
public class AmCabinetService
{
    @Autowired
    private AmCabinetMapper amCabinetMapper;

    /**
     * 查询档案柜
     * 
     * @param id 档案柜主键
     * @return 档案柜
     */
    public AmCabinet selectAmCabinetById(Long id)
    {
        return amCabinetMapper.selectAmCabinetById(id);
    }

    /**
     * 查询档案柜列表
     * 
     * @param amCabinet 档案柜
     * @return 档案柜
     */
    public List<AmCabinet> selectAmCabinetList(AmCabinet amCabinet)
    {
        //amCabinet.setTenantId(SecurityUtils.getTenantId());
        return amCabinetMapper.selectAmCabinetList(amCabinet);
    }

    /**
     * 新增档案柜
     * 
     * @param amCabinet 档案柜
     * @return 结果
     */
    public int insertAmCabinet(AmCabinet amCabinet)
    {
        amCabinet.setCreateTime(DateUtils.getNowDate());
        amCabinet.setCreateBy(SecurityUtils.getUsername());
        //amCabinet.setTenantId(SecurityUtils.getTenantId());

        amCabinetMapper.insertAmCabinet(amCabinet);
        if(amCabinet.getParentId()!=null){
            AmCabinet parent = amCabinetMapper.selectAmCabinetById(amCabinet.getParentId());
            if(parent!=null){
                amCabinet.setCpath((parent.getCpath()==null?"":parent.getCpath())+"/"+parent.getId());
                amCabinet.setNpath((parent.getNpath()==null?"":parent.getNpath())+"/"+parent.getName());
            }
        }
        return amCabinetMapper.updateAmCabinet(amCabinet);
    }

    /**
     * 修改档案柜
     * 
     * @param amCabinet 档案柜
     * @return 结果
     */
    public int updateAmCabinet(AmCabinet amCabinet)
    {
        return amCabinetMapper.updateAmCabinet(amCabinet);
    }

    /**
     * 批量删除档案柜
     * 
     * @param ids 需要删除的档案柜主键
     * @return 结果
     */
    public int deleteAmCabinetByIds(Long[] ids)
    {
        return amCabinetMapper.deleteAmCabinetByIds(ids);
    }

    /**
     * 删除档案柜信息
     * 
     * @param id 档案柜主键
     * @return 结果
     */
    public int deleteAmCabinetById(Long id)
    {
        return amCabinetMapper.deleteAmCabinetById(id);
    }




    /**
     * 构建前端所需要下拉树结构
     *
     * @param depts 部门列表
     * @return 下拉树结构列表
     */
    public List<CabinetTreeSelect> buildTreeSelect(List<AmCabinet> depts)
    {
        List<AmCabinet> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(CabinetTreeSelect::new).collect(Collectors.toList());
    }


    /**
     * 构建前端所需要树结构
     *
     * @param datas 部门列表
     * @return 树结构列表
     */
    public List<AmCabinet> buildDeptTree(List<AmCabinet> datas)
    {
        List<AmCabinet> returnList = new ArrayList<AmCabinet>();
        List<Long> tempList = new ArrayList<Long>();
        for (AmCabinet dept : datas)
        {
            tempList.add(dept.getId());
        }
        for (AmCabinet dept : datas)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFn(datas, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = datas;
        }
        return returnList;
    }


    private void recursionFn(List<AmCabinet> list, AmCabinet t)
    {
        // 得到子节点列表
        List<AmCabinet> childList = getChildList(list, t);
        t.setChildren(childList);
        for (AmCabinet tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<AmCabinet> getChildList(List<AmCabinet> list, AmCabinet t)
    {
        List<AmCabinet> tlist = new ArrayList<AmCabinet>();
        Iterator<AmCabinet> it = list.iterator();
        while (it.hasNext())
        {
            AmCabinet n = (AmCabinet) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<AmCabinet> list, AmCabinet t)
    {
        return getChildList(list, t).size() > 0;
    }
}
